/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.sml.SEFM.DAO;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.RollbackException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.SystemException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Query;
import org.hibernate.Transaction;
import vn.com.sml.SEFM.domain.Cities;
import vn.com.sml.SEFM.domain.Countries;
import vn.com.sml.SEFM.domain.Merchant;
import vn.com.sml.SEFM.domain.Cardblacklist;
import vn.com.sml.SEFM.domain.Ip;
import vn.com.sml.SEFM.util.HibernateUtil;
/**
 *
 * @author Tunp
 */
public class CardBlackDAO {
  public static Integer saveCardBlacklist(Cardblacklist CardBlacklist) throws SecurityException, RollbackException, HeuristicRollbackException, HeuristicMixedException, IllegalStateException, SystemException {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Integer CardBlacklistId = null;
        try {
            transaction = (Transaction) session.beginTransaction();
            
            CardBlacklistId = (Integer) session.save(CardBlacklist);
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return CardBlacklistId;
    }
    
    public static List<Cardblacklist> listCardBlacklist() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<Cardblacklist> lst = new ArrayList<Cardblacklist>();
        try {
            transaction = (Transaction) session.beginTransaction();
            
            List lstUR = session.createQuery("from Cardblacklist").list();
            for (Iterator iterator = (Iterator) lstUR.iterator();iterator.hasNext();)
            {
                Cardblacklist CardBlack = (Cardblacklist) iterator.next();
                Merchant dvcnt = (Merchant) session.createQuery("select i.merchant from Cardblacklist as i where i.id=" + CardBlack.getId()).uniqueResult();

                CardBlack.setMerchant(dvcnt);
                lst.add(CardBlack);
            }
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return lst;
    }
    
    public static Boolean DeleteBlackCard(int id)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Boolean flag = false;
        try {
            transaction = (Transaction) session.beginTransaction();
            Query query = session.createQuery("delete from Cardblacklist as i where id="+id);
            int row = query.executeUpdate();
              if (row == 0){
                  System.out.println("Doesn't deleted any row!");
              }
              else{
                  System.out.println("DeletedRow: " + row);
              }
            flag = true;
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return flag;
    }

    public static List<Cardblacklist> listCardBlacklist(String search_col, String search_content) {
    Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<Cardblacklist> lst = new ArrayList<Cardblacklist>();
        
        if(search_col.equals("merchant")) {
            search_col = "merchant.merchantName";
        }
        try {
            transaction = (Transaction) session.beginTransaction();
            
            List lstUR = session.createQuery("from Cardblacklist where "+search_col+" like '%"+search_content+"%'").list();
            for (Iterator iterator = (Iterator) lstUR.iterator();iterator.hasNext();)
            {
                Cardblacklist CardBlack = (Cardblacklist) iterator.next();
                Merchant dvcnt = (Merchant) session.createQuery("select i.merchant from Cardblacklist as i where i.id=" + CardBlack.getId()).uniqueResult();
                CardBlack.setMerchant(dvcnt);
                lst.add(CardBlack);
            }
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return lst;
    }
}
